package com.jige.jigetodo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jige.jigetodo.common.BaseResponse;
import com.jige.jigetodo.common.ErrorCode;
import com.jige.jigetodo.common.ResultUtils;
import com.jige.jigetodo.exception.ThrowUtils;
import com.jige.jigetodo.model.dto.user.*;
import com.jige.jigetodo.model.entity.User;
import com.jige.jigetodo.model.vo.UserVO;
import com.jige.jigetodo.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/register")
    public BaseResponse<Boolean> userRegister(@RequestBody UserRegisterRequest userRegisterRequest,HttpServletRequest request) throws IOException {
        ThrowUtils.throwIf(userRegisterRequest == null, ErrorCode.PARAMS_ERROR, "参数异常");
        String account = userRegisterRequest.getUsername();
        String password = userRegisterRequest.getPassword();
        String code = userRegisterRequest.getCode();
        String checkPassword = userRegisterRequest.getCheckPassword();
        boolean result = userService.register(account, password, checkPassword,code,request);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "注册失败");
        return ResultUtils.success(true);
    }

    @PostMapping("/login")
    public BaseResponse<UserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userLoginRequest == null, ErrorCode.PARAMS_ERROR, "参数异常");
        String account = userLoginRequest.getUsername();
        String password = userLoginRequest.getPassword();
        String code = userLoginRequest.getCode();
        UserVO userVO = userService.userLogin(account, password, code,request);
        ThrowUtils.throwIf(userVO == null, ErrorCode.OPERATION_ERROR, "登录失败");
        return ResultUtils.success(userVO);
    }

    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        boolean result = userService.userLogOut(request);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "退出失败，系统错误");
        return ResultUtils.success(true);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> userUpdate(@RequestBody UserUpdateRequest userUpdateRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userUpdateRequest == null, ErrorCode.PARAMS_ERROR, "参数异常");
        boolean result = userService.updateUser(userUpdateRequest, request);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "更新失败");
        return ResultUtils.success(true);
    }

    @PostMapping("/resetPassword")
    public BaseResponse<Boolean> userResetPassword(@RequestBody UserResetPasswordRequest userResetPasswordRequest) {
        ThrowUtils.throwIf(userResetPasswordRequest == null, ErrorCode.PARAMS_ERROR, "参数异常");
        String userAccount = userResetPasswordRequest.getUserAccount();
        String oldPassword = userResetPasswordRequest.getOldPassword();
        String newPassword = userResetPasswordRequest.getNewPassword();
        String checkPassword = userResetPasswordRequest.getCheckPassword();
        boolean result = userService.resetPassword(userAccount, oldPassword, newPassword, checkPassword);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "重置失败");
        return ResultUtils.success(true);
    }


    @GetMapping("/get")
    public BaseResponse<User> userGetUserById(Long id, HttpServletRequest request) {
        ThrowUtils.throwIf(id == null, ErrorCode.PARAMS_ERROR);
        User user = userService.getUserById(id, request);
        ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR, "用户不存在");
        return ResultUtils.success(user);
    }

    @GetMapping("/get/vo")
    public BaseResponse<UserVO> userGetUserVOById(Long id, HttpServletRequest request) {
        ThrowUtils.throwIf(id == null, ErrorCode.PARAMS_ERROR);
        UserVO userVO = userService.getUserVOById(id, request);
        ThrowUtils.throwIf(userVO == null, ErrorCode.NOT_FOUND_ERROR, "用户不存在");
        return ResultUtils.success(userVO);
    }

    @PostMapping("/list/page")
    public BaseResponse<Page<User>> userGetUserListByPage(@RequestBody UserQueryRequest userQueryRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userQueryRequest == null, ErrorCode.PARAMS_ERROR);
        int current = userQueryRequest.getCurrent();
        int pageSize = userQueryRequest.getPageSize();
        //  限制爬虫
        ThrowUtils.throwIf(pageSize > 20, ErrorCode.PARAMS_ERROR);
        Page<User> userPage = userService.page(new Page<>(current, pageSize), userService.getQueryWrapper(userQueryRequest));
        return ResultUtils.success(userPage);
    }

    @PostMapping("/list/page/vo")
    public BaseResponse<Page<UserVO>> userGetUserVOListByPage(@RequestBody UserQueryRequest userQueryRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userQueryRequest == null, ErrorCode.PARAMS_ERROR);
        int current = userQueryRequest.getCurrent();
        int pageSize = userQueryRequest.getPageSize();
        //  限制爬虫
        ThrowUtils.throwIf(pageSize > 20, ErrorCode.PARAMS_ERROR);
        Page<User> userPage = userService.page(new Page<>(current, pageSize), userService.getQueryWrapper(userQueryRequest));
        Page<UserVO> userVOPage = new Page<>(current, pageSize, userPage.getTotal());
        List<UserVO> userVOList = userService.listUserVOByPage(userPage.getRecords(), request);
        userVOPage.setRecords(userVOList);
        return ResultUtils.success(userVOPage);
    }

    @GetMapping("/get/login")
    public BaseResponse<User> userGetLoginUser(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(loginUser);
    }

    @GetMapping("/get/login/VO")
    public BaseResponse<UserVO> userGetLoginUserVO(HttpServletRequest request) {
        UserVO loginUser = userService.getLoginUserVO(request);
        return ResultUtils.success(loginUser);
    }
}
